package com.jxd.service.impl;

import com.jxd.entity.MesContent;
import com.jxd.entity.MesHead;
import com.jxd.hbase.utils.QurryTableUntil;
import com.jxd.hbase.utils.UpdateSendDeviceName;
import com.jxd.model.SwitchCover;
import com.jxd.repositories.MesHeadRepositories;
import com.jxd.service.SwitchCoverMapperService;
import com.jxd.service.SwitchCoverService;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SwitchCoverServiceImpl implements SwitchCoverService {
    @Autowired
    @Qualifier("mesHeadRepositories")
    MesHeadRepositories mesHeadRepositories;
    @Autowired
    @Qualifier("getHtableByMesHead")
    HTable mesHeadClient;
    @Autowired
    @Qualifier("getHtableByMesContent")
    HTable mesContentClient;
    @Autowired
    private UpdateSendDeviceName updateSendDeviceName;
    @Autowired
    private SwitchCoverMapperService switchCoverMapper;

    @Override
    public List<MesHead> getCommMess(String fileName, String mesId, String shipId) {

        List<MesHead> mesCommHeads = mesHeadRepositories.findAllByFileNameAndMesIdAndShipId(fileName, mesId, shipId);
        return mesCommHeads;
    }

    @Override
    /**
     * 根据61632报文查询所有的F维护报文
     */
    public List<MesHead> getMaintainMess(String fileName, String mesId, String shipid) {
        List<MesHead> mesHeads = mesHeadRepositories.findAllByFileNameAndMesIdAndShipId(fileName, mesId, shipid);

        return mesHeads;
    }

    @Override
    public List<SwitchCover> getMaintainMessSwitchCoverUnitNumber(List<MesHead> maintainMess) {
        List<SwitchCover> switchCovers = new ArrayList<>();
        for (int i = 0; i < maintainMess.size(); i++) {
            String rowkey_2 = maintainMess.get(i).getMesSignId() + "-2";
            Result rs = null;
            try {
                //获取维护项目
                Get get = new Get(Bytes.toBytes(rowkey_2));
                rs = mesContentClient.get(get);
                //获取第6行报文
                String val = Bytes.toString(rs.getValue(Bytes.toBytes("f1"), Bytes.toBytes("code")))
                        .replace("'", "");
                System.out.println(val);
                if (val.matches("03.*F")) {
                    //开盖 0 ----1
                    SwitchCover switchCover = new SwitchCover();
                    String SendDevice = updateSendDeviceName.
                            updateSendDeviceName(maintainMess.get(i).getShipId(),
                                    maintainMess.get(i).getSendDevice());
                    String dymc = SendDevice + val.substring(2, 3) + "0";
                    switchCover.setDymc(dymc);
                    switchCover.setType("1");
                    switchCover.setMoshi("0");
                    switchCovers.add(switchCover);
                } else if (val.matches("03.*0")) {
                    //关盖 1----1
                    SwitchCover switchCover = new SwitchCover();
                    String SendDevice = updateSendDeviceName.
                            updateSendDeviceName(maintainMess.get(i).getShipId(),
                                    maintainMess.get(i).getSendDevice());
                    String dymc = SendDevice + val.substring(2, 3) + "0";
                    switchCover.setDymc(dymc);
                    switchCover.setType("1");
                    switchCover.setMoshi("1");
                    switchCovers.add(switchCover);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeStream(mesContentClient);
            }
        }
        return switchCovers;
    }
    @Override
    public List<SwitchCover> qurryMaintainSwitchCoverss(List<MesHead> maintainMess, Connection connection, String tableName) {
        List<SwitchCover> switchCovers = new ArrayList<>();
        // 结果集合
        List<MesContent> vallist= new ArrayList<>();
        //rowKeys
        List<MesContent> rowkeys = new ArrayList<>();
        for (int i = 0; i < maintainMess.size(); i++) {
            String rowkey_2 = maintainMess.get(i).getMesSignId() + "-2";
            MesContent mesContent = new MesContent();
            mesContent.setRowkey(rowkey_2);
            mesContent.setMesSignId(maintainMess.get(i).getMesSignId());
            mesContent.setMesDate(maintainMess.get(i).getMesDate());
            String SendDevice = updateSendDeviceName.
                    updateSendDeviceName(maintainMess.get(i).getShipId(),
                            maintainMess.get(i).getSendDevice());
            mesContent.setSendDevice(SendDevice);
            rowkeys.add(mesContent);
        }
        //查询结果
        QurryTableUntil qurryTableUntil = new QurryTableUntil();
        vallist = qurryTableUntil.qurryTimeAndVal(rowkeys, tableName, connection);
        for (int i = 0; i <vallist.size() ; i++) {
            String val = vallist.get(i).getCode();
            if (val.matches("03.*F")) {
                //开盖 0 ----1
                SwitchCover switchCover = new SwitchCover();
                String SendDevice = updateSendDeviceName.
                        updateSendDeviceName(maintainMess.get(i).getShipId(),
                                maintainMess.get(i).getSendDevice());
                String dymc = SendDevice + val.substring(2, 3) + "0";
                System.out.println(dymc);
                switchCover.setDymc(dymc);
                switchCover.setType("1");
                switchCover.setMoshi("0");
                switchCovers.add(switchCover);

            } else if (val.matches("03.*0")) {
                //关盖 1----1
                SwitchCover switchCover = new SwitchCover();
                String SendDevice = updateSendDeviceName.
                        updateSendDeviceName(maintainMess.get(i).getShipId(),
                                maintainMess.get(i).getSendDevice());
                String dymc = SendDevice + val.substring(2, 3) + "0";

                System.out.println(dymc);

                switchCover.setDymc(dymc);
                switchCover.setType("1");
                switchCover.setMoshi("1");
                switchCovers.add(switchCover);
            }
        }
        return switchCovers;
    }


    /**
     * 获取控制报文的数组
     * @param CommMess
     * @param connection
     * @param tableName
     * @return
     */
    @Override
    public List<SwitchCover> qurryCommMessSwitchCover(List<MesHead> CommMess, Connection connection, String tableName) {
        List<MesContent> rowkeys_6 = new ArrayList<>();
        List<MesContent> rowkeys_5 = new ArrayList<>();
        List<MesContent> rowkeys_4 = new ArrayList<>();
        List<MesContent> rowkeys_3 = new ArrayList<>();
        List<MesContent> rowkeys_2 = new ArrayList<>();
        List<SwitchCover> switchCovers = new ArrayList<>();
        //rowkey-6数组
        List<MesContent> vallist_6 = new ArrayList<>();
        //rowkey-5数组
        List<MesContent> vallist_5 = new ArrayList<>();
        //rowkey-4数组
        List<MesContent> vallist_4 = new ArrayList<>();
        //rowkey-3数组
        List<MesContent> vallist_3 = new ArrayList<>();
        //rowkey-2数组
        List<MesContent> vallist_2 = new ArrayList<>();

        for (int i = 0; i < CommMess.size(); i++) {
            //设置rowkeys-6
            String rowkey_6 = CommMess.get(i).getMesSignId() + "-6";
            MesContent mesContent = new MesContent();
            mesContent.setRowkey(rowkey_6);
            mesContent.setMesSignId(CommMess.get(i).getMesSignId());
            rowkeys_6.add(mesContent);
            //设置rowkeys-5
            String rowkey_5 = CommMess.get(i).getMesSignId() + "-5";
            MesContent mesContent5 = new MesContent();
            mesContent5.setRowkey(rowkey_5);
            mesContent5.setMesSignId(CommMess.get(i).getMesSignId());
            rowkeys_5.add(mesContent5);
            //设置rowkeys-4
            String rowkey_4 = CommMess.get(i).getMesSignId() + "-4";
            MesContent mesContent4 = new MesContent();
            mesContent4.setRowkey(rowkey_4);
            mesContent4.setMesSignId(CommMess.get(i).getMesSignId());
            rowkeys_4.add(mesContent4);
            //设置rowkeys-3
            String rowkey_3 = CommMess.get(i).getMesSignId() + "-3";
            MesContent mesContent3 = new MesContent();
            mesContent3.setRowkey(rowkey_3);
            mesContent3.setMesSignId(CommMess.get(i).getMesSignId());
            rowkeys_3.add(mesContent);
            //设置rowkeys-2
            String rowkey_2 = CommMess.get(i).getMesSignId() + "-2";
            MesContent mesContent2 = new MesContent();
            mesContent2.setRowkey(rowkey_2);
            mesContent2.setMesSignId(CommMess.get(i).getMesSignId());
            rowkeys_2.add(mesContent2);
        }
        QurryTableUntil qurryTableUntil = new QurryTableUntil();
        vallist_6 = qurryTableUntil.qurryTimeAndVal(rowkeys_6, tableName, connection);
        vallist_5 = qurryTableUntil.qurryTimeAndVal(rowkeys_5, tableName, connection);
        vallist_4 = qurryTableUntil.qurryTimeAndVal(rowkeys_4, tableName, connection);
        vallist_3 = qurryTableUntil.qurryTimeAndVal(rowkeys_3, tableName, connection);
        vallist_2 = qurryTableUntil.qurryTimeAndVal(rowkeys_2, tableName, connection);
        for (int i = 0; i <vallist_6.size() ; i++) {
            String val = vallist_6.get(i).getCode();
            if (val.equals("D0FF")) {
                //获取库号
                String val5 = vallist_5.get(i).getCode().substring(3, 4);
                //获取单元号
                String val4 = vallist_4.get(i).getCode().substring(1, 2);
                //获取模块号
                String val3 = vallist_3.get(i).getCode().substring(3, 4);

                //获取单类型
                String val2 = vallist_2.get(i).getCode().substring(3, 4);
                SwitchCover switchCover = new SwitchCover();
                //拼接单元序号
                String dymc = val5 + val4 + val3 + val2;
                switchCover.setDymc(dymc);
                //设置开关盖模式
                switchCover.setMoshi("0");
                switchCover.setType("0");
                switchCover.setLastoperationtime(CommMess.get(i).getMesDate());
                switchCovers.add(switchCover);
            } else if (val.equals("D000")) {
                //关盖 D000
                String val5 = vallist_5.get(i).getCode().substring(3, 4);
                //获取单元号
                String val4 = vallist_4.get(i).getCode().substring(1, 2);
                //获取模块号
                String val3 = vallist_3.get(i).getCode().substring(3, 4);
                //获取单类型
                String val2 = vallist_2.get(i).getCode().substring(3, 4);
                SwitchCover switchCover = new SwitchCover();
                //拼接单元序号
                String dymc = val5 + val4 + val3 + val2;
                System.out.println(dymc);
                if (dymc.equals("11F1")){
                    System.out.println(vallist_3.get(i).getCode());
                    break;
                }
                switchCover.setDymc(dymc);
                //设置开关盖模式
                switchCover.setMoshi("1");
                switchCover.setType("0");
                switchCover.setLastoperationtime(CommMess.get(i).getMesDate());
                switchCovers.add(switchCover);

            }
        }
        return switchCovers;
    }



    @Override
    public void insertSwitchCover(List<SwitchCover> switchCovers, String xtid,String shipid) {
        for (int i = 0; i < switchCovers.size(); i++) {
            switchCovers.get(i).setXtid(xtid);
            switchCoverMapper.insertSwitchCover(switchCovers.get(i));
        }
    }

}
